package com.example.onlinemonitoring.controller;


import com.alibaba.fastjson.JSONObject;
import com.example.onlinemonitoring.model.Dtspartition;
import com.example.onlinemonitoring.model.Dtsphistory;
import com.example.onlinemonitoring.model.Dtssensor;
import com.example.onlinemonitoring.model.Dtssensorandpartition;
import com.example.onlinemonitoring.dao.AppalarmMapper;
import com.example.onlinemonitoring.dao.DtspartitionMapper;
import com.example.onlinemonitoring.model.Appalarm;
import com.example.onlinemonitoring.model.Dtshistory;
import com.example.onlinemonitoring.model.common.MyResult;
import com.example.onlinemonitoring.model.common.chartres;
import com.example.onlinemonitoring.model.common.pagebase;
import com.example.onlinemonitoring.query.QueryObject;
import com.example.onlinemonitoring.serviceImpl.DtssensorServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
/**
 * Controller
 * 该部分代码为自动生成，请勿随意修改
 * 如有改动请删除该注释
 * @author little pig page
 * @date 2022-03-28
 */
@Api(value = "分布式光纤设备", tags = { "分布式光纤设备" })
@RestController
@RequestMapping("/distributedTemperatureSensing")
public class DtssensorController {
    Logger logger = LogManager.getLogger(LogManager.ROOT_LOGGER_NAME);
    @Autowired
    DtssensorServiceImpl dtssensorServiceImpl;
    @Autowired
    DtspartitionMapper dtspartitionmapper;
    @Autowired
    AppalarmMapper appalarmmapper;

    @ApiOperation(value = "查询分区表数据", notes = "查询分区表数据")
    @ResponseBody
    @GetMapping("/selectPartitionData")
    public MyResult<Dtspartition> selectPartitionData(
            @RequestParam(value = "stationid", required = false) Integer stationid,
            @RequestParam(value = "devicegroup", required = false) Integer devicegroup,
            @RequestParam(value = "ipaddr", required = false) String ipaddr,
            @RequestParam(value = "chunk", required = false) Integer chunk,
            @RequestParam(value = "partitionno", required = false) Integer partitionno
    ) {
        Dtspartition record = new Dtspartition();
        record.setStationid(stationid);
        record.setDevicegroup(devicegroup);
        record.setIpaddr(ipaddr);
        record.setChunk(chunk);
        record.setPartitionno(partitionno);
        return dtssensorServiceImpl.getList(record);
    }

    @ApiOperation(value="修改通道名称", notes= "修改通道名称")
    @ResponseBody
    @RequestMapping(value = "/updateChunkName", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public MyResult<Integer> updateChunkName(@RequestBody String jsonParam,
                                             @RequestParam(value = "id", required = true) Long id
    ) {
        JSONObject object  = JSONObject.parseObject(jsonParam);
        MyResult<Integer> m = new MyResult<Integer>();
        Dtssensor record = new Dtssensor();
        record.setId(id);
        record.setPointname(object.getString("pointname"));
        Dtssensor dts = dtssensorServiceImpl.selectByPrimaryKey(record);
        if(dts == null){
            m.setMsg("修改失败");
            m.setCode(1);
        }else{
            if(dtssensorServiceImpl.Update(record,true) >= 1){
                Dtspartition dtsp = new Dtspartition();
                dtsp.setStationid(dts.getStationid());
                dtsp.setDevicegroup(dts.getDevicegroup());
                dtsp.setIpaddr(dts.getIpaddr());
                dtsp.setChunk(dts.getChunk());
                dtsp.setPointname(record.getPointname());
                dtssensorServiceImpl.Update(dtsp,false);
                m.setMsg("修改成功");
                m.setCode(0);
            }else{
                m.setMsg("修改失败");
                m.setCode(1);
            }
        }
        return m;
    }
    @ApiOperation(value="修改通道内部光纤长度", notes= "修改通道内部光纤长度")
    @ResponseBody
    @GetMapping("/updateChunkInternalLength")
    public MyResult<Integer> updateChunkInternalLength(            
        @RequestParam(value = "id", required = true) Long id,
        @RequestParam(value = "machinelength", required = true) Integer machinelength) {
        MyResult<Integer> m = new MyResult<Integer>();
        Dtssensor record = new Dtssensor();
        record.setId(id);
        record.setMachinelength(machinelength);
        if(dtssensorServiceImpl.Update(record,true) >= 1){
            m.setMsg("操作成功！");
            m.setCode(0);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
        }else{
            m.setMsg("操作失败，通道不存在！");
            m.setCode(1);
        }
        return m;
    }
    @ApiOperation(value="修改分区", notes= "修改分区")
    @ResponseBody
    @RequestMapping(value = "/updatePartition", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public MyResult<Integer> updatePartition(@RequestBody String jsonParam,
                                             @RequestParam(value = "id", required = true) Long id,
                                             @RequestParam(value = "deviceid", required = true) Integer deviceid) {
        JSONObject object  = JSONObject.parseObject(jsonParam);
        MyResult<Integer> m = new MyResult<Integer>();
        Dtspartition record = new Dtspartition();
        record.setId(id);
        record.setChunk(object.getInteger("chunk"));
        record.setDifferalarmrelievevalue(object.getFloat("differalarmrelievevalue"));
        record.setDifferalarmvalue(object.getFloat("differalarmvalue"));
        record.setDifferwarnrelievevalue(object.getFloat("differwarnrelievevalue"));
        record.setDifferwarnvalue(object.getFloat("differwarnvalue"));
        record.setEndposition(object.getFloat("endposition"));
        record.setIpaddr(object.getString("ipaddr"));
        record.setIpname(object.getString("ipname"));
        record.setStationid(object.getInteger("stationid"));
        record.setDevicegroup(object.getInteger("devicegroup"));
        record.setIpaddr(object.getString("ipaddr"));
        record.setPartitionno(object.getInteger("partitionno"));
        record.setPartitionname(object.getString("partitionname"));
        record.setPointname(object.getString("pointname"));
        record.setStartposition(object.getFloat("startposition"));
        record.setTemperalarmrelievevalue(object.getFloat("temperalarmrelievevalue"));
        record.setTemperalarmvalue(object.getFloat("temperalarmvalue"));
        record.setTemperwarnrelievevalue(object.getFloat("temperwarnrelievevalue"));
        record.setTemperwarnvalue(object.getFloat("temperwarnvalue"));
        record.setMachinelength(object.getInteger("machinelength"));
                                     
        if(dtssensorServiceImpl.Update(record,true) >= 1){
            m.setMsg("操作成功");
            m.setCode(0);
            Dtspartition dtsp = dtspartitionmapper.selectByPrimaryKey(record);
            if(dtsp.getStartposition()!= record.getStartposition()||dtsp.getEndposition()!=record.getEndposition()){
                Example ex = new Example(Appalarm.class);
                Example.Criteria criteria = ex.createCriteria();
                criteria.andEqualTo("stationid",record.getStationid());
                criteria.andEqualTo("devicegroup",record.getDevicegroup());
                criteria.andEqualTo("deviceid",deviceid);
                criteria.andLike("alarmcolumn", "分区"+record.getPartitionname()+"位置%");
                appalarmmapper.deleteByExample(ex);
            }
        }else{
            m.setMsg("操作失败");
            m.setCode(1);
        }
        return m;
    }

    @ApiOperation(value="新增分区", notes= "新增分区")
    @ResponseBody
    @RequestMapping(value = "/addPartition", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public MyResult<Integer> addPartition(@RequestBody String jsonParam,
                                          @RequestParam(value = "id", required = true) Long id) {
        JSONObject object  = JSONObject.parseObject(jsonParam);
        MyResult<Integer> m = new MyResult<Integer>();
        Dtspartition record = new Dtspartition();
        record.setChunk(object.getInteger("chunk"));
        record.setDifferalarmrelievevalue(object.getFloat("differalarmrelievevalue"));
        record.setDifferalarmvalue(object.getFloat("differalarmvalue"));
        record.setDifferwarnrelievevalue(object.getFloat("differwarnrelievevalue"));
        record.setDifferwarnvalue(object.getFloat("differwarnvalue"));
        record.setEndposition(object.getFloat("endposition"));
        record.setIpaddr(object.getString("ipaddr"));
        record.setIpname(object.getString("ipname"));
        record.setStationid(object.getInteger("stationid"));
        record.setDevicegroup(object.getInteger("devicegroup"));
        record.setIpaddr(object.getString("ipaddr"));
        record.setPartitionno(object.getInteger("partitionno"));
        record.setPartitionname(object.getString("partitionname"));
        record.setPointname(object.getString("pointname"));
        record.setStartposition(object.getFloat("startposition"));
        record.setTemperalarmrelievevalue(object.getFloat("temperalarmrelievevalue"));
        record.setTemperalarmvalue(object.getFloat("temperalarmvalue"));
        record.setTemperwarnrelievevalue(object.getFloat("temperwarnrelievevalue"));
        record.setTemperwarnvalue(object.getFloat("temperwarnvalue"));
        if(dtssensorServiceImpl.getModel(record) == null){
            if(dtssensorServiceImpl.add(record) >= 1){
                m.setMsg("操作成功");
                m.setCode(0);
            }else{
                m.setMsg("操作失败");
                m.setCode(1);
            }
        }else{
            m.setMsg("分区编号重复");
            m.setCode(1);
        }
        return m;
    }

    @ApiOperation(value = "删除分区", notes = "删除分区")
    @ResponseBody
    @GetMapping("/deletePartition")
    public MyResult<Integer> deletePartition(@RequestParam(value = "id", required = true) Long id) {
        MyResult<Integer> m = new MyResult<Integer>();
        if(dtssensorServiceImpl.pDelete(id) >= 1){
            m.setMsg("操作成功");
            m.setCode(0);
        }else{
            m.setMsg("操作失败");
            m.setCode(1);
        }
        return m;
    }


    @ApiOperation(value="修改设备参数", notes= "修改设备参数")
    @ResponseBody
    @RequestMapping(value = "/updateAll", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public MyResult<Integer> updateAll(@RequestBody String jsonParam,
        @RequestParam(value = "stationid", required = false) Integer stationid,
        @RequestParam(value = "deviceaddr", required = false) Integer deviceaddr,
        @RequestParam(value = "devicegroup", required = false) Integer devicegroup) {
        JSONObject object  = JSONObject.parseObject(jsonParam);
        Dtssensor record = object.toJavaObject(Dtssensor.class);
        MyResult<Integer> m = new MyResult<Integer>();
        record.setStationid(stationid);
        record.setDevicegroup(devicegroup);
        //record.setDeviceaddr(deviceaddr);
        if(dtssensorServiceImpl.Update(record,false) >= 1){
            m.setMsg("修改成功");
            m.setCode(0);
        }else{
            m.setMsg("修改失败");
            m.setCode(1);
        }
        return m;
    }
    @ApiOperation(value="修改设备报警", notes= "修改设备报警")
    @ResponseBody
    @RequestMapping(value = "/updateAlarm", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public MyResult<Integer> updateAlarm(@RequestBody String jsonParam,
        @RequestParam(value = "stationid", required = false) Integer stationid,
        @RequestParam(value = "deviceaddr", required = false) Integer deviceaddr,
        @RequestParam(value = "devicegroup", required = false) Integer devicegroup) {
        JSONObject object  = JSONObject.parseObject(jsonParam);
        MyResult<Integer> m = new MyResult<Integer>();
        Dtssensor record = object.toJavaObject(Dtssensor.class);
        record.setStationid(stationid);
        record.setDevicegroup(devicegroup);
        //record.setDeviceaddr(deviceaddr);
        if(dtssensorServiceImpl.Update(record,false) >= 1){
            m.setMsg("修改成功");
            m.setCode(0);
        }else{
            m.setMsg("修改失败");
            m.setCode(1);
        }
        return m;
    }
    @ApiOperation(value = "查询实时表数据", notes = "查询实时表数据")
    @ResponseBody
    @GetMapping("/selectSensorData")
    public MyResult<Dtssensorandpartition> selectSensorData(
    @RequestParam(value = "page", required = false) Integer page,
        @RequestParam(value = "limit", required = false) Integer limit,
        @RequestParam(value = "stationid", required = false) Integer stationid,
        @RequestParam(value = "devicegroup", required = false) Integer devicegroup,
        @RequestParam(value = "ipaddr", required = false) String ipaddr,
        @RequestParam(value = "chunk", required = false) Integer chunk
    ) {
        Dtssensor record1 = new Dtssensor();
        record1.setStationid(stationid);
        record1.setDevicegroup(devicegroup);
        record1.setChunk(chunk);
        QueryObject qo = new QueryObject(page,limit);
        List<Dtssensor> list1=dtssensorServiceImpl.getLists(record1,qo);

        Dtspartition record2 = new Dtspartition();
        record2.setStationid(stationid);
        record2.setDevicegroup(devicegroup);
        record2.setIpaddr(ipaddr);
        record2.setChunk(chunk);
        dtssensorServiceImpl.getList(record2);
        List<Dtspartition> list2=dtssensorServiceImpl.getList(record2).getData();

        Dtssensorandpartition record3 = new Dtssensorandpartition();
        record3.setDtspartition(list2);
        record3.setDtssensor(list1);
        MyResult<Dtssensorandpartition> res = new MyResult<>();
        List<Dtssensorandpartition> list3 = new ArrayList<Dtssensorandpartition>();
        list3.add(record3);
        res.setData(list3);
        return res;
    }
    @ApiOperation(value="修改监测点名称", notes= "修改监测点名称")
    @ResponseBody
    @RequestMapping(value = "/updateMonitorName", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public MyResult<Integer> updateMonitorName(@RequestBody String jsonParam,
        @RequestParam(value = "id", required = false) Long id
    ) {
        JSONObject object  = JSONObject.parseObject(jsonParam);
        MyResult<Integer> m = new MyResult<Integer>();
        Dtssensor record = new Dtssensor();
        record.setId(id);
        record.setPointname(object.getString("pointname"));
        if( dtssensorServiceImpl.Update(record,true) >= 1){
            m.setMsg("修改成功");
            m.setCode(0);
        }else{
            m.setMsg("修改失败");
            m.setCode(1);
        }
        return m;
    }
    @ApiOperation(value = "查询分区历史表数据", notes = "查询分区历史表数据")
    @ResponseBody
    @GetMapping("/selectPartitionHistoryData")
    public MyResult<Dtsphistory> selectPartitionHistoryData(
    @RequestParam(value = "page", required = true) Integer page,
            @RequestParam(value = "limit", required = true) Integer limit,
            @RequestParam(value = "stationid", required = false) Integer stationid,
            @RequestParam(value = "devicegroup", required = false) Integer devicegroup,
            @RequestParam(value = "ipaddr", required = false) String ipaddr,
            @RequestParam(value = "chunk", required = false) Integer chunk,
            @RequestParam(value = "partitionno", required = false) Integer partitionno,
            @RequestParam(value = "start", required = false) String begintime,
            @RequestParam(value = "end", required = false) String endtime,
            @RequestParam(value = "pagecount", required = false) Integer pagecount,
            @RequestParam(value = "pid", required = false) Long pid) {
        Dtsphistory record = new Dtsphistory();
        record.setStationid(stationid);
        record.setIpaddr(ipaddr);
        record.setChunk(chunk);
        record.setPid(pid);
        record.setPartitionno(partitionno);
        record.setDevicegroup(devicegroup);
        pagebase pagebase = new pagebase();
        pagebase.setBegintime(begintime);
        pagebase.setEndtime(endtime);
        pagebase.setPage(page);
        pagebase.setLimit(limit);
        pagebase.setPagecount(pagecount);
        return dtssensorServiceImpl.getPHisList(record,pagebase);
    }
    @ApiOperation(value = "查询历史表数据", notes = "查询历史表数据")
    @ResponseBody
    @GetMapping("/selectHistoryData")
    public MyResult<Dtshistory> selectHistoryData(
            @RequestParam(value = "page", required = true) Integer page,
            @RequestParam(value = "limit", required = true) Integer limit,
            @RequestParam(value = "stationid", required = false) Integer stationid,
            @RequestParam(value = "devicegroup", required = false) Integer devicegroup,
            @RequestParam(value = "ipaddr", required = false) String ipaddr,
            @RequestParam(value = "chunk", required = false) Integer chunk,
            @RequestParam(value = "start", required = false) String begintime,
            @RequestParam(value = "end", required = false) String endtime,
            @RequestParam(value = "pagecount", required = false) Integer pagecount) {
        Dtshistory record = new Dtshistory();
        record.setStationid(stationid);
        record.setIpaddr(ipaddr);
        record.setChunk(chunk);
        record.setDevicegroup(devicegroup);
        pagebase pagebase = new pagebase();
        pagebase.setBegintime(begintime);
        pagebase.setEndtime(endtime);
        pagebase.setPage(page);
        pagebase.setLimit(limit);
        pagebase.setPagecount(pagecount);
        return dtssensorServiceImpl.getHisList(record,pagebase);
    }
}